package com.song.composejoke.model;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.song.composejoke.network.NetworkUtil;

import java.io.Serializable;
import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableEmitter;
import io.reactivex.rxjava3.core.ObservableOnSubscribe;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class NewsModel implements Serializable {
    /**
     * uniquekey : db61b977d9fabd0429c6d0c671aeb30e
     * title : “新时代女性的自我关爱”主题沙龙暨双山街道福泰社区妇儿活动家园启动仪式举行
     * date : 2021-03-08 13:47:00
     * category : 头条
     * author_name : 鲁网
     * url : https://mini.eastday.com/mobile/210308134708834241845.html
     * thumbnail_pic_s : https://dfzximg02.dftoutiao.com/news/20210308/20210308134708_d0216565f1d6fe1abdfa03efb4f3e23c_0_mwpm_03201609.png
     * thumbnail_pic_s02 : https://dfzximg02.dftoutiao.com/news/20210308/20210308134708_d0216565f1d6fe1abdfa03efb4f3e23c_1_mwpm_03201609.png
     * thumbnail_pic_s03 : https://dfzximg02.dftoutiao.com/news/20210308/20210308134708_d0216565f1d6fe1abdfa03efb4f3e23c_2_mwpm_03201609.png
     * is_content : 1
     */

    public String uniquekey;
    public String title;
    public String date;
    public String category;
    public String author_name;
    public String url;
    public String thumbnail_pic_s;
    public String thumbnail_pic_s02;
    public String thumbnail_pic_s03;
    public String is_content;

    public static void getData(int start, Listener listener) {
        Observable.create(new ObservableOnSubscribe<List<NewsModel>>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<List<NewsModel>> emitter) throws Throwable {
                long time = System.currentTimeMillis() / 1000;
                String json = NetworkUtil.getJSON("https://v.juhe.cn/toutiao/index?type=top&page=" + start +
                        "&page_size=10&is_filter=1&key=46cc9617064fc351f9a62d46d59d4aba\n");
                Log.e("NewsModel", json);

                Gson gson = new Gson();
                try {
                    JsonObject jsonObject = gson.fromJson(json, JsonObject.class);
                    JsonObject resultObject = (JsonObject) jsonObject.get("result");
                    String data = resultObject.get("data").toString();
                    List<NewsModel> list = gson.fromJson(data, new TypeToken<List<NewsModel>>() {
                    }.getType());
                    emitter.onNext(list);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<NewsModel>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                    }

                    @Override
                    public void onNext(@NonNull List<NewsModel> list) {
                        listener.onDataLoaded(list);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }

    public interface Listener {
        void onDataLoaded(List<NewsModel> list);
    }
}
